package com.example.demo.msg.aio.server;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class AcceptHandle implements CompletionHandler<AsynchronousSocketChannel,AioServerHandle> {

    @Override
    public void completed(AsynchronousSocketChannel channel, AioServerHandle aioServerHandle) {
        AioServer.clientCount++;
        System.out.println("连接的客户端数：" + AioServer.clientCount);

        aioServerHandle.serverSocketChannel.accept(aioServerHandle,this);

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //异步读  第三个参数为接收消息回调的业务Handler
        channel.read(buffer, buffer, new ReadHandle(channel));

    }

    @Override
    public void failed(Throwable throwable, AioServerHandle aioServerHandle) {
        throwable.printStackTrace();
        aioServerHandle.latch.countDown();


    }
}